<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="时间" prop="customDate">
                    <el-radio-group v-model="queryParams.customDate" @change="changeCustomDate">
                        <el-radio-button :label="1">今天</el-radio-button>
                        <el-radio-button :label="2">昨天</el-radio-button>
                        <el-radio-button :label="3">近7天</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="日期" prop="date">
                    <el-date-picker v-model="queryParams.date" format="yyyy-MM-dd" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"></el-date-picker>
                </el-form-item>
                <el-form-item label="供应商名称" prop="supply_name">
                    <SelectPage v-model="queryParams.supply_id" placeholder="请选择供应商" :url="$api.supply.getSupplyList" show_field="supply_name" query_field="supply_name" query_field2="spu" filterable clearable popper-class="custom-select" style="width: 240px" ></SelectPage>
                    <!-- <el-input v-model.trim="queryParams.supply_name" placeholder="请输入供应商名称" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" /> -->
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="card">
            <el-row :gutter="10" class="mb8">
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="供应商" prop="supply_links" width="500">
                    <template slot-scope="scope" v-if="scope.row.supply_links.length > 0">
                        <div>
                            <template v-for="(item, index) in scope.row.supply_links">
                                <div :key="index" v-if="index < 3">
                                    <!-- <el-tooltip effect="dark" :content="item.supply_link" placement="top"> -->
                                        <!-- <el-link type="primary" @click="toSupply(item.supply_link)"> -->
                                            <span>
                                                {{ item.supply_name }}
                                                <span v-if="index < scope.row.supply_links.length - 1">，</span>
                                                <span v-if="scope.row.supply_links.length > 3">...</span>
                                            </span>
                                        <!-- </el-link> -->
                                    <!-- </el-tooltip> -->
                                    
                                </div>
                            </template>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="SPU" prop="spu" min-width="160">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showInfoDialog(scope.row)">{{ scope.row.spu }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="产品图片" min-width="100">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.product_images" :readOnly="true"></ImageUploadImg>
                    </template>
                </el-table-column>
                <el-table-column label="销量" prop="skuquantity" min-width="160"></el-table-column>
                <el-table-column label="订单时间" prop="date" min-width="160"></el-table-column>
                <el-table-column label="操作" width="160">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-search" @click="AfterSalesReasons(scope.row)">售后原因</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <!-- <supply-sale-info-dialog v-model="infoDialog.visible" v-if="infoDialog.visible" :params="infoDialog.data"></supply-sale-info-dialog> -->
        <sku-info-dialog v-model="infoDialog.visible" v-if="infoDialog.visible" :params="infoDialog.data"></sku-info-dialog>
        
        <SpuReasonsDialog v-model="rankDialogReasons.visible" v-if="rankDialogReasons.visible" :params="rankDialogReasons.data"></SpuReasonsDialog>
    </div>
</template>

<script>
import { querySupplySaleList } from '@/api/supply'
import { parseTime } from '@/utils/quickadmin'
import SupplySaleInfoDialog from '../components/SupplySaleInfoDialog.vue'
import SkuInfoDialog from '../components/SkuInfoDialog.vue'
import SpuReasonsDialog from '../components/SpuReasonsDialog.vue'
export default {
    name: 'SupplySaleList',
    components: { SupplySaleInfoDialog, SkuInfoDialog,SpuReasonsDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                supply_name: undefined,
                date: [],
                customDate: 2,
                
                shop_id: undefined,
                customDate: 2,
                develop_type: undefined,
            },
            infoDialog: {
                visible: false,
                data: {},
            },
            
            rankDialogReasons: {
                visible: false,
                data: {}
            },
        }
    },
        watch: {
        'queryParams.date': {
            handler(val) {
                if (val?.length == 2) {
                    this.queryParams.customDate = 0
                }
            },
            immediate: true,
            deep: true,
        },
    },
    created() {
        this.getList()
    },
    methods: {
        AfterSalesReasons(row) {
            this.rankDialogReasons.visible = true
            this.rankDialogReasons.data.spu = row.spu
            this.rankDialogReasons.data.customDate = this.queryParams.customDate
            this.rankDialogReasons.data.date = this.queryParams.date
        },
        changeCustomDate() {
            this.queryParams.date = []
            this.handleQuery()
        },
        showInfoDialog(row) {
            this.infoDialog.visible = true
            this.infoDialog.data = row
        },
        toSupply(url) {
            window.open(url, '_blank')
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date?.length == 2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m-d'), parseTime(qyparams.date[1], 'y-m-d')]
            }
            querySupplySaleList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>

